package org.ytor.core.sqlflow.dsl.eval.function.support;

import org.ytor.common.util.Numbers;
import org.ytor.core.sqlflow.dsl.eval.function.Func;
import org.ytor.core.sqlflow.dsl.expr.node.support.FunctionCallNode;

import java.util.List;

/**
 * created by yangtong on 2025/8/7 00:01:48
 * <br/>
 * 去除字符串两边的空字符
 */
public class NumFunc implements Func {
    @Override
    public Boolean support(FunctionCallNode functionCallNode) {
        return functionCallNode.functionName.equalsIgnoreCase("number");
    }

    @Override
    public Object call(List<Object> args) {
        if (args.size() != 1) {
            throw new IllegalArgumentException("number函数只能有一个参数：number(string)");
        }
        Object arg = args.getFirst();
        if (arg == null) {
            return null;
        }
        if (!(arg instanceof String)) {
            throw new IllegalArgumentException("number函数的参数必须是字符串，当前是【" + arg.getClass().getSimpleName() + "】");
        }

        return Numbers.toNumber(arg.toString());
    }
}
